﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI;
using System.Web.UI.WebControls;
using WEB.BLL.Entities;
using WEB.Exceptions;
using WEB.BLL.Services;
using WEB.BLL;
using Ext.Net;
using WEB.Core.Util;
using WEB.Core.Controls;

namespace WEB.APP.Controls.System
{
    public partial class UserForm : BaseControl
    {
        public event EventHandler SaveClick;
        private void OnSaveClick()
        {
            if (SaveClick != null)
            {
                SaveClick(this, EventArgs.Empty);
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            BindingStoreRole();
        }
        private void BindingStoreRole()
        {
            RoleService roleS = RoleService.GetInstance();
            IList<RoleEntity> lstRole = roleS.LoadAll();
            if (null != lstRole)
            {
                storeRole.DataSource = lstRole;
                storeRole.DataBind();
            }
        }
        public void Show(string username, bool isEdit)
        {
            UserEntity userE = new UserEntity();
            UserService userS = UserService.GetInstance();
            userE.UserName = username;
            userE = userS.LoadByPrimaryKey(userE);
            if (null == userE)
                return;
            hdfUserName.Text = userE.UserName;
            txtUserName.Text = userE.UserName;
            txtUserName.Disabled = true;
            txtPassword.Text = userE.Password;
            hdfPassword.Text = userE.Password;
            txtConfirmPassword.Text = userE.Password;
            cbbRole.SetValue(userE.RoleId);
            ckbIsActive.Checked = userE.IsActive;
            btnSave.Hidden = !IsEdit;
            winDetails.Show();
            txtPassword.Focus(true, 50);
        }
        public void Show()
        {
            hdfUserName.Text = string.Empty;
            FormPanel1.Reset();
            txtUserName.Disabled = false;
            cbbRole.SelectedIndex = 0;
            btnSave.Hidden = !IsAdd;
            winDetails.Show();
            txtUserName.Focus(true, 50);
        }
        protected void btnSave_Click(object sender, DirectEventArgs e)
        {
            try
            {
                UserEntity userE = new UserEntity();
                UserService userS = UserService.GetInstance();
                userE.UserName = txtUserName.Text;
                if (hdfPassword.Text != txtPassword.Text)
                    userE.Password = Encryption.StringToMD5Hash(txtPassword.Text);
                else
                    userE.Password = hdfPassword.Text;
                userE.RoleId = Format.ConvertToInt(cbbRole.SelectedItem.Value);
                userE.IsActive = ckbIsActive.Checked;
                userE.CreatedDate = userE.UpdatedDate = DateTime.Now;
                userE.CreatedBy = userE.UpdatedBy = WEBContext.Current.User.UserName;
                if (!string.IsNullOrEmpty(hdfUserName.Text))
                {
                    userS.Update(userE);
                }
                else
                {
                    userS.Insert(userE);
                }
                winDetails.Hide();
                OnSaveClick();

            }
            catch (Exception ex)
            {
                Notification(ex);
            }
        }
    }
}